package com.bw.lib_net

import androidx.lifecycle.LiveData
import com.bw.lib_net.interceptor.HttpHeadInterceptor
import com.bw.lib_net.interceptor.SignInterceptor
import com.bw.lib_net.interceptor.TokenInterceptor
import dagger.Module
import dagger.Provides
import dagger.Reusable
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.util.concurrent.TimeUnit
import javax.inject.Qualifier
import javax.inject.Singleton

/**
 * @author ytx
 * @date 2025-09-16 14:46
 * @description
 * 注意：一个模块中不能存在多个返回值相同的函数，若相同通过限定符区分
 */
@Module
@InstallIn(SingletonComponent::class)
class NetModule {
    /**
     * 提供公共OkHttpClient：添加三个拦截器
     */
    @Provides
    @Singleton
    @CommonOkHttpClient
    fun getOkhttpClient():OkHttpClient{
        return OkHttpClient.Builder()
            .readTimeout(10, TimeUnit.SECONDS)
            .writeTimeout(10, TimeUnit.SECONDS)
            .addInterceptor(HttpHeadInterceptor())
            .addInterceptor(SignInterceptor())
            .addInterceptor(TokenInterceptor())
            .addInterceptor(HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
            .build()
    }

    @Provides
    @Singleton
    @SignOkHttpClient
    fun getSignOkhttpClient():OkHttpClient{
        return OkHttpClient.Builder()
            .readTimeout(10, TimeUnit.SECONDS)
            .writeTimeout(10, TimeUnit.SECONDS)
            .addInterceptor(HttpHeadInterceptor())
            .addInterceptor(SignInterceptor())
            .addInterceptor(HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
            .build()
    }

    @Provides
    @Singleton
    @TokenOkHttpClient
    fun getTokenOkhttpClient():OkHttpClient{
        return OkHttpClient.Builder()
            .readTimeout(10, TimeUnit.SECONDS)
            .writeTimeout(10, TimeUnit.SECONDS)
            .addInterceptor(HttpHeadInterceptor())
            .addInterceptor(TokenInterceptor())
            .addInterceptor(HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
            .build()
    }

    @Provides
    @Singleton
    @CommonRetrofit
    fun getRetrofit(@CommonOkHttpClient okHttpClient: OkHttpClient):Retrofit{
        return Retrofit.Builder()
            .baseUrl("http://10.161.9.80:7037/")
            .client(okHttpClient)
            .addConverterFactory(GsonConverterFactory.create())
            .build()
    }

    @Provides
    @Singleton
    @SignRetrofit
    fun getSignRetrofit(@SignRetrofit okHttpClient: OkHttpClient):Retrofit{
        return Retrofit.Builder()
            .baseUrl("")
            .client(okHttpClient)
            .addConverterFactory(GsonConverterFactory.create())
            .build()
    }

    @Provides
    @Singleton
    @TokenRetrofit
    fun getTokenRetrofit(@TokenOkHttpClient okHttpClient: OkHttpClient):Retrofit{
        return Retrofit.Builder()
            .baseUrl("")
            .client(okHttpClient)
            .addConverterFactory(GsonConverterFactory.create())
            .build()
    }

    //按baseUrl划分：业务

    @Provides
    @Singleton
    @MallRetrofit
    fun getMallRetrofit(@CommonOkHttpClient okHttpClient: OkHttpClient):Retrofit{
        return Retrofit.Builder()
            .baseUrl("http://MALL接口")
            .client(okHttpClient)
            .addConverterFactory(GsonConverterFactory.create())
            .build()
    }



    /**限定符*/
    @Qualifier
    @Retention(AnnotationRetention.RUNTIME)
    annotation class CommonOkHttpClient

    @Qualifier
    @Retention(AnnotationRetention.RUNTIME)
    annotation class SignOkHttpClient

    @Qualifier
    @Retention(AnnotationRetention.RUNTIME)
    annotation class TokenOkHttpClient


    @Qualifier
    @Retention(AnnotationRetention.RUNTIME)
    annotation class CommonRetrofit

    @Qualifier
    @Retention(AnnotationRetention.RUNTIME)
    annotation class SignRetrofit


    @Qualifier
    @Retention(AnnotationRetention.RUNTIME)
    annotation class TokenRetrofit


    //商城部分接口
    @Qualifier
    @Retention(AnnotationRetention.RUNTIME)
    annotation class MallRetrofit




}